package com.ysd.ziyuanshiyongtongji.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ysd.ziyuanshiyongtongji.dao.IMemberDao;
import com.ysd.ziyuanshiyongtongji.dto.MemberDTO;
import com.ysd.ziyuanshiyongtongji.model.Member;
import com.ysd.ziyuanshiyongtongji.service.IMemberService;
import com.ysd.ziyuanshiyongtongji.util.POIUtil;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

/**
 * MemberController
 */
@ApiOperation("会员列表页")
@RestController
@RequestMapping("/member")
public class MemberController {
@Resource
private IMemberService memberService;
@Resource
private IMemberDao memberDao;
@ApiOperation(value = "分页查询",notes = "分页查询")
@PostMapping("/findPage")
@GetMapping("/findPage")
public Map<String,Object> findPage(@RequestBody MemberDTO dto){
    Page<Member> page=memberService.findpage(dto);
    Map<String, Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        map.put("count",page.getNumberOfElements());
        map.put("msg", "查询成功");
        map.put("data", page.getContent());
		return map;
}
@ApiOperation(value = "保存会员信息",notes = "添加或修改")
@PostMapping("/addOrEdit")
public Member saveMember(@RequestBody Member m){
    return memberService.saveMember(m);
}
@ApiOperation(value = "批量添加",notes="该功能还未实现")
@PostMapping(value = "/batch")
public int batchAdd(){
 //path 参数是前台页面上传时的文件名，这里使用本地文件模拟下
 String path = "C:\\Users\\asus\\Desktop\\会员登记表.xlsx";
  List<Member> list=new ArrayList<Member>();
  try {
      list =POIUtil.importMember(path);
  } catch (IOException e) {
     e.printStackTrace();
  }
  return memberService.addAll(list);
}
@ApiOperation(value = "删除添加",notes = "删除会员")
//@ApiImplicitParam(name="id",value = "主键",required = true,paramType ="path" )
@DeleteMapping("/{id}")
public void del(@PathVariable Integer id){
    memberService.del(id);
} 
 @ApiOperation(value = "删除",notes = "删除")
@DeleteMapping(value = "/delete")
public void deletebatch(@RequestBody String id){
   System.out.println(id);
   String[] str=id.split(",");
   System.out.println(str.toString());
   List<Integer> list=new ArrayList<>();
   for(String l : str){
   Integer i=Integer.valueOf(l);
    list.add(i);
   }
   memberService.deletebatch(list);
}  
@ApiOperation(value = "批量删除会员",notes = "批量删除会员")
    @PostMapping("/delAll")
    public void delAll(@RequestBody List<Member> list){
        System.out.println(list);
        memberService.delAll(list);
    }
    @ApiOperation(value = "单元格修改")
    @PutMapping(value = "/editTable/{status}/{id}")
    public int editTable(@PathVariable String status,@PathVariable Integer id){
     return memberDao.editTable(status, id);
    }
}